// mircoApp/competitionApply/applyOrder/applyOrder.js
const app = getApp();
import Dialog from "../../../dist/dialog/dialog";
const skinBehavior = require("../../../utils/themeSkin/skinBehavior");
Page({
  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    query:{
      student_keyword:'',
      status:'',
      page:1,
    },
    tabList: [
      {
        status: ' ',
        index: 0,
        name: "全部",
        
      },
      {
        status: 0,
        index: 1,
        name: "待付款",
       
      },
      {
        status: 3,
        index: 3,
        name: "已完成",
        
      },
      {
        status: -1,
        index: 4,
        name: "已关闭",
      }
    ],
    orderList:[],
    totalPage:0,
    isLoadMore: true,
    timer:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
  },
  onOrderChange(event) {
    console.log(event);
    let {name} = event.detail;
    this.setData({
      'query.page':1,
      'query.status':name,
      isLoadMore:true,
      orderList:[],
    });
    this.goodsList();
  },
  onSearch(event) {
    console.log(event);
    this.setData({
      'query.student_keyword':event.detail,
      'query.page':1,
      isLoadMore:true,
      orderList:[]
    })
    this.goodsList();
  },
  orderstateOn(event) {
    let {item,itembtn} = event.currentTarget.dataset;
    console.log(item);
    console.log(itembtn);
    let {id} = item;
    let {value,api} = itembtn
    //:"/packageD/member/myOrder/Aftersales/Aftersales?refund_id=" + item.id +"&pluginid=" + item.plugin_id"
    let query = {
      1: `/packageD/buy/orderPay/orderPay?order_id=${id}&status=2`, //支付
      41:  `${api}`, //客服
      13:`/packageD/member/myOrder/refund/refund?order_id=${id}&type=1`,
      18:`/packageD/member/myOrder/Aftersales/Aftersales?refund_id=${item.refund_id}`,
      'my_score':`/mircoApp/competitionApply/myResult/myResult?id=${id}`,
      'apply_msg':`/mircoApp/competitionApply/gameDetails/gameDetails?orderIds=${id}`
    };
    console.log(value.toString() === '9');
    if(value.toString() === '9' || value.toString() === '12') {
      console.log('asdfasdfsadfsdf');
      Dialog.confirm({
        title: '提示',
        message: value===9? '确定取消该订单?':'确定删除该订单?',
      }).then(() => {
        // on close
        console.log('dddddddddddd');
        value==9?this.cancelPay(id,api):this.deletePay(id,api);
      })
      .catch(() => {

        // on cancel
      });  //删除订单
    } else {
      return wx.navigateTo({
        url: query[value]
      });
    }
  },
  cancelPay(id,api) {
    let urlStr = app.getNetAddresss(api);
    let json = {
      order_id:id
    };
    app._postNetWork({
      url: urlStr,
      data:json,
      success:  (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
          this.setData({
            'query.page':1,
            isLoadMore:true,
            orderList:[],
          });
          this.goodsList();
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  deletePay(id,api) {
    let urlStr = app.getNetAddresss(api);
    let json = {
      order_id:id
    };
    app._postNetWork({
      url: urlStr,
      data:json,
      success:  (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
          this.setData({
            'query.page':1,
            isLoadMore:true,
            orderList:[],
          });
          this.goodsList();
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getorderState() {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      'query.page':1,
      isLoadMore:true,
      orderList:[],
    });
    this.goodsList();
  },
  goodsList() {
    let urlStr = app.getNetAddresss("plugin.event-registration.api.order.order.index");
    app._getNetWork({
      url: urlStr,
      data:this.data.query,
      success:  (resdata) => {
        let res = resdata.data;
        let data = res.data;
        wx.hideLoading();
        wx.hideNavigationBarLoading();//隐藏导航条加载动画
        //停止下拉刷新
        wx.stopPullDownRefresh();
        if (res.result == 1) {
          console.log(data);
          this.data.totalPage = data.last_page;
          if (!this.data.totalPage) {
            this.data.totalPage = 0;
          }
          this.setData({
            orderList: data.data,
          })
          console.log(this.data.orderList);
        } else {
          this.data.query.page = this.data.query.page - 1;
          this.data.isLoadMore = false;
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
   //获取更多数据
   getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.query.page >= this.data.totalPage) {
      return;
    } else {
      this.data.query.page = this.data.query.page + 1;
      let urlStr = app.getNetAddresss("plugin.event-registration.api.order.order.index");
      app._getNetWork({
        url: urlStr,
        data: this.data.query,
        success: (resdata) => {
          let res = resdata.data;
          let list = res.data.data;
          console.log(list);
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              orderList : this.data.orderList.concat(list),
            })
          } else {
            this.data.page = this.data.page - 1;
            this.isLoadMore = false;
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    wx.showNavigationBarLoading();
    //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
      wx.showLoading({
      title: '刷新中...',
    })
    this.setData({
      'query.page':1,
      orderList:[],
      isLoadMore: true,
    })
    this.goodsList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})